/**
 * creater:han.wen@dhc.com.cn
 * create time:2016-06-20 10:50:35
 * describe:游戏设置
 */
APP.controller('BallSettingController', ['$scope', 'BallSettingService', '$ionicModal', '$state', 'PopupService',
  function ($scope, BallSettingService, $ionicModal, $state, PopupService) {

    /***变量声明***/
    $scope.data = undefined;//比赛信息
    $scope.modal = undefined;//弹窗选择
    $scope.modalData = undefined;//弹窗信息
    $scope.member = undefined;//球员信息

    /**
     * 初始化函数
     */
    $scope.init = function () {
      //初始化弹窗
      $ionicModal.fromTemplateUrl('templates/common/Modal.html', {
        scope: $scope
      }).then(function (modal) {
        $scope.modal = modal;
      });
      $scope.data = {
        field: '',//场地
        weather: '',//天气
        team: [{//球队1
          name: '',//队名
          member: ''//队员
        }, {//球队2
          name: '',
          member: ''
        }],
        type: '',//比赛类型-友谊赛
        time: ''//比赛时间
      };
      $scope.modalData = {
        title: '',//弹窗标题
        content: '',//内容
        id: '',//加载信息种类
        type: '',//哪只球队
        isSelect: ''//哪只球队被选择
      };
      $scope.member = {
        flag: [],//队员是否被选中
        number: 0,//队员选中个数
        isGoalKeeper: false,//是否有守门员被选中
        defender: 0,//防守者个数
        attacker: 0//进攻者个数
      };
    };

    /**
     * 加载设置条件
     */
    $scope.setting = function (index, itemId) {
      if (index == 0) {
        $scope.modalData.title = '场地';
        $scope.modalData.content = BallSettingService.getField();//获取数据集合
        $scope.modalData.type = -1;//无球队区分
      } else if (index == 1) {
        $scope.modalData.title = '天气';
        $scope.modalData.content = BallSettingService.getWeather();//获取数据集合
        $scope.modalData.type = -1;//无球队区分
      } else if (index == 2) {
        $scope.modalData.title = '球队';
        $scope.modalData.content = BallSettingService.getTeam();//获取数据集合
        $scope.modalData.type = itemId;//球队赋值
      } else if (index == 3) {
        if (!$scope.data.team[itemId].name) {//选择球员之前先选球队
          PopupService.showToast('请先选择球队' + (itemId + 1));
          return;
        }
        $scope.modalData.title = '球员';
        $scope.modalData.content = BallSettingService.getMember($scope.data.team[itemId].name);//获取数据集合
        $scope.modalData.type = itemId;//球队赋值
        loadMemberSelect(itemId);//球员选中bool值加载
      } else if (index == 4) {
        $scope.modalData.title = '性质';
        $scope.modalData.content = BallSettingService.getType();//获取数据集合
        $scope.modalData.type = -1;//无球队区分
      } else if (index == 5) {
        $scope.modalData.title = '时间';
        $scope.modalData.content = BallSettingService.getTime();//获取数据集合
        $scope.modalData.type = -1;//无球队区分
      }
      $scope.modalData.id = index;//记录数据种类
      $scope.modal.show();//弹窗打开
    };

    /**
     * 选择结果
     */
    $scope.select = function (data) {
      var modalData = $scope.modalData;
      if (modalData.id == 0) {
        $scope.data.field = data;//所选内容赋值data
      } else if (modalData.id == 1) {
        $scope.data.weather = data;//所选内容赋值data
      } else if (modalData.id == 2) {
        $scope.modalData.isSelect = $scope.data.team[modalData.type == 0 ? 1 : 0].name;//将另一支球队的名字赋给标志位，防止球队重复选择
        if (modalData.isSelect == data) {
          PopupService.showToast('不允许选择相同的球队，请重新选择');
          return;
        }
        $scope.data.team[modalData.type].name = data;//所选内容赋值data
      } else if (modalData.id == 3) {
        $scope.data.team[modalData.type].member = data;//所选内容赋值data
      } else if (modalData.id == 4) {
        $scope.data.type = data;//所选内容赋值data
      } else if (modalData.id == 5) {
        $scope.data.time = data;//所选内容赋值data
      }
      $scope.modal.hide();//弹窗隐藏
      $scope.member = {//球员选择完清除对象，防止数据复用
        flag: [],
        number: 0,
        isGoalKeeper: false,
        defender: 0,
        attacker: 0
      };
    };

    /**
     * 队员选拔
     */
    $scope.memberSelect = function (index, data) {
      if (data) {//正常选择
        if (!$scope.member.flag[index]) {//球员未被选择，点击选中球员
          if (data.position == 'GK' && $scope.member.isGoalKeeper) {//重复选择守门员
            PopupService.showToast('队伍已经有守门员，请选择其他球员');
            return;
          }
          if ($scope.member.number == 11) {//不能选择超过十一人
            PopupService.showToast('已选择11人，若选择其他球员，先取消勾选再重新选择');
            return;
          }
          if ($scope.member.number == 10 && data.position != 'GK' && !$scope.member.isGoalKeeper) {//球队不能没有守门员
            //for (var i = 0; i < $scope.modalData.content.length; i++) {
            //  if ($scope.modalData.content[i].position == 'GK') {
            //    $scope.member.flag[i] = !$scope.member.flag[i];
            //    break;
            //  }
            //}
            PopupService.showToast('队伍没有守门员，请选择守门员');
            return;
          }
          $scope.member.flag[index] = !$scope.member.flag[index];//当前球员由未选中变为选中状态
          $scope.member.number++;//选中人数+1
        } else {//球员已被选择，点击取消选中
          $scope.member.flag[index] = !$scope.member.flag[index];//当前球员由选中变为未选中状态
          $scope.member.number--;//选中人数-1
        }
        if (data.position == 'GK') {//当前队员是守门员，需要将守门员标志位置为true
          $scope.member.isGoalKeeper = !$scope.member.isGoalKeeper;
        }
      } else {//自动选拔
        for (var i = 0; i < $scope.modalData.content.length; i++) {//循环所有球员
          if ($scope.member.isGoalKeeper) {//当前守门员已被选中
            if ($scope.member.defender == 5) {//当前防守者已够5人
              if ($scope.member.attacker == 5) {//当前进攻者已够5人
                break;//退出循环
              } else {//当前进攻者不够5人
                if ($scope.modalData.content[i].position == 'CM' || $scope.modalData.content[i].position == 'RM' || $scope.modalData.content[i].position == 'LM' ||
                  $scope.modalData.content[i].position == 'RW' || $scope.modalData.content[i].position == 'LW' || $scope.modalData.content[i].position == 'CAM' ||
                  $scope.modalData.content[i].position == 'ST' || $scope.modalData.content[i].position == 'CF') {//当前位置是前场位置，让其选中
                  $scope.member.flag[i] = !$scope.member.flag[i];
                  $scope.member.attacker++;
                  $scope.member.number++;
                }
              }
            } else {//当前防守者不够5人
              if ($scope.modalData.content[i].position == 'SW' || $scope.modalData.content[i].position == 'CB' || $scope.modalData.content[i].position == 'LB' ||
                $scope.modalData.content[i].position == 'RB' || $scope.modalData.content[i].position == 'CDM') {//当前位置是后场位置，让其选中
                $scope.member.flag[i] = !$scope.member.flag[i];
                $scope.member.defender++;
                $scope.member.number++;
              }
            }
          } else {//当前守门员没有选中
            if ($scope.modalData.content[i].position == 'GK') {//当前位置是守门员位置，让其选中
              $scope.member.flag[i] = !$scope.member.flag[i];
              $scope.member.isGoalKeeper = !$scope.member.isGoalKeeper;
              $scope.member.number++;
            }
          }
        }
      }
    };

    /**
     * 完成选拔
     */
    $scope.memberFinish = function () {//当选中队员正好十一人时，触发完成选拔按钮
      var data = [];
      for (var i = 0; i < $scope.modalData.content.length; i++) {
        if ($scope.member.flag[i]) {
          data.push($scope.modalData.content[i]);//对应选中bool和队员
        }
      }
      $scope.select(data);//调用选择结果
    };

    /**
     * 开始游戏
     */
    $scope.start = function () {
      if (!$scope.data.field) {//进行一系列校验，不通过则弹窗提示
        PopupService.showToast('请选择场地');
      } else if (!$scope.data.weather) {
        PopupService.showToast('请选择天气');
      } else if (!$scope.data.team[0].name) {
        PopupService.showToast('请选择玩家球队');
      } else if (!$scope.data.team[0].member) {
        PopupService.showToast('请选择玩家球员');
      } else if (!$scope.data.team[1].name) {
        PopupService.showToast('请选择电脑球队');
      } else if (!$scope.data.team[1].member) {
        PopupService.showToast('请选择电脑球员');
      } else if (!$scope.data.type) {
        PopupService.showToast('请选择性质');
      } else if (!$scope.data.time) {
        PopupService.showToast('请选择时间');
      } else {
        var data = $scope.data;
        data.time = data.time.substr(0, 1);//时间格式化
        $state.go('ballGame', {settingData: JSON.stringify(data)});//跳转到比赛页面
      }
    };

    /**
     * 球员选中bool值加载
     */
    var loadMemberSelect = function (index) {
      for (var i = 0; i < $scope.data.team[index].member.length; i++) {
        $scope.member.flag.push(false);
      }
    };
    /*
     $scope.init();//如果页面缓存则用此方法初始化
     */
    $scope.$on('$ionicView.beforeEnter', function () {
      $scope.init();
    });
  }
])
;

/**
 * creater:han.wen@dhc.com.cn
 * create time:2016-06-20 10:50:35
 * describe:游戏设置
 */
APP.service('BallSettingService', [function () {
  /**
   * 场地
   */
  this.getField = function () {
    return ['温布利球场', '法兰西球场', '安菲尔德球场', '诺坎普球场'];
  };

  /**
   * 天气
   */
  this.getWeather = function () {
    return ['晴', '多云', '小雨', '小雪'];
  };

  /**
   * 球队
   */
  this.getTeam = function () {
    return ['拜仁慕尼黑', '巴塞罗那'];
    //return ['拜仁慕尼黑', '巴塞罗那', '曼联', '尤文图斯'];
  };

  /**
   * 球员
   */
  this.getMember = function (team) {
    if (team == '拜仁慕尼黑') {
      return [{
        name: '诺伊尔',
        age: '',
        position: 'GK',
        level: '90',
        role: ''
      }, {
        name: '乌尔赖希',
        age: '',
        position: 'GK',
        level: '75',
        role: ''
      }, {
        name: '贝内蒂亚',
        age: '',
        position: 'CB',
        level: '85',
        role: ''
      }, {
        name: '巴德斯图贝尔',
        age: '',
        position: 'CB',
        level: '83',
        role: ''
      }, {
        name: '博阿滕',
        age: '',
        position: 'CB',
        level: '89',
        role: ''
      }, {
        name: '胡梅尔斯',
        age: '',
        position: 'CB',
        level: '90',
        role: ''
      }, {
        name: '拉姆',
        age: '',
        position: 'RB',
        level: '88',
        role: ''
      }, {
        name: '拉菲尼亚',
        age: '',
        position: 'RB',
        level: '82',
        role: ''
      }, {
        name: '贝纳尔特',
        age: '',
        position: 'LB',
        level: '84',
        role: ''
      }, {
        name: '阿拉巴',
        age: '',
        position: 'LB',
        level: '89',
        role: ''
      }, {
        name: '比达尔',
        age: '',
        position: 'CDM',
        level: '89',
        role: ''
      }, {
        name: '哈维马丁内斯',
        age: '',
        position: 'CDM',
        level: '85',
        role: ''
      }, {
        name: '蒂亚戈',
        age: '',
        position: 'CM',
        level: '86',
        role: ''
      }, {
        name: '格策',
        age: '',
        position: 'CAM',
        level: '87',
        role: ''
      }, {
        name: '哈维阿隆索',
        age: '',
        position: 'CM',
        level: '88',
        role: ''
      }, {
        name: '罗本',
        age: '',
        position: 'RM',
        level: '87',
        role: ''
      }, {
        name: '里贝里',
        age: '',
        position: 'LM',
        level: '87',
        role: ''
      }, {
        name: '科斯塔',
        age: '',
        position: 'LW',
        level: '86',
        role: ''
      }, {
        name: '穆勒',
        age: '',
        position: 'CF',
        level: '91',
        role: ''
      }, {
        name: '莱万多夫斯基',
        age: '',
        position: 'ST',
        level: '90',
        role: ''
      }, {
        name: '科曼',
        age: '',
        position: 'ST',
        level: '83',
        role: ''
      }]
    } else if (team == '巴塞罗那') {
      return [{
        name: '布拉沃',
        age: '',
        position: 'GK',
        level: '85',
        role: ''
      }, {
        name: '特尔施特根',
        age: '',
        position: 'GK',
        level: '82',
        role: ''
      }, {
        name: '马蒂厄',
        age: '',
        position: 'CB',
        level: '84',
        role: ''
      }, {
        name: '皮克',
        age: '',
        position: 'CB',
        level: '90',
        role: ''
      }, {
        name: '马斯切拉诺',
        age: '',
        position: 'CB',
        level: '89',
        role: ''
      }, {
        name: '达尼阿尔维斯',
        age: '',
        position: 'RB',
        level: '86',
        role: ''
      }, {
        name: '阿德里亚诺',
        age: '',
        position: 'LB',
        level: '80',
        role: ''
      }, {
        name: '阿尔巴',
        age: '',
        position: 'RB',
        level: '87',
        role: ''
      }, {
        name: '布斯克茨',
        age: '',
        position: 'CDM',
        level: '90',
        role: ''
      }, {
        name: '拉基蒂奇',
        age: '',
        position: 'CDM',
        level: '89',
        role: ''
      }, {
        name: '伊涅斯塔',
        age: '',
        position: 'CM',
        level: '91',
        role: ''
      }, {
        name: '罗伯托',
        age: '',
        position: 'CM',
        level: '85',
        role: ''
      }, {
        name: '梅西',
        age: '',
        position: 'RW',
        level: '94',
        role: ''
      }, {
        name: '苏亚雷斯',
        age: '',
        position: 'ST',
        level: '92',
        role: ''
      }, {
        name: '内马尔',
        age: '',
        position: 'LW',
        level: '92',
        role: ''
      }, {
        name: '穆尼尔',
        age: '',
        position: 'LW',
        level: '83',
        role: ''
      }, {
        name: '桑德罗',
        age: '',
        position: 'RW',
        level: '82',
        role: ''
      }]
    }
  };

  /**
   * 性质
   */
  this.getType = function (index) {
    return ['友谊赛', '邀请赛', '国家联赛', '欧洲冠军联赛'];
  };

  /**
   * 时间
   */
  this.getTime = function () {
    return ['3分钟', '6分钟'];
  };

}]);
